﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
using System.IO;
using System.Drawing;
public partial class administrator_uc_admin_a_category : WebPageSecurity
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //LoadDataToDropDownList(DropdownParentID, 0);      
        }
    }   

    //private void LoadDataToDropDownList(DropDownList cboName, int CateID)
    //{
    //    ListItem list = new ListItem();
    //    cboName.Items.Clear();
    //    cboName.Items.Add(list);
    //    list.Selected = true;
    //    list.Value = "0";
    //    list.Text = "---Chọn danh mục cha---";
    //    DataTable dtb = new CmsBLL().Category_GetTree(CateID);
    //    if (dtb.Rows.Count > 0)
    //    {
    //        new CommonBLL().LoadDropDonwList(cboName, dtb, "_Title", "CateID");
    //    }
    //}

    protected void lbtAddNewItem_Click(object sender, EventArgs e)
    {
        try
        {
            string strFileName = string.Empty;
            if (FileUploadImage.HasFile)
            {
                try
                {
                    Stream fStream = FileUploadImage.FileContent;
                    strFileName = FileUploadImage.FileName.Replace(' ', '_');
                    strFileName = strFileName.Substring(strFileName.LastIndexOf("\\") + 1);
                    strFileName = CommonBLL.GetFilename(strFileName);

                    string filepathsmallimage = Server.MapPath("" + DAL.EnvInfo.PathUpload + "smallimages//") + strFileName;

                    // Validate the uploaded file
                    // 1. The uploaded file should not be a blank file
                    if (FileUploadImage.FileBytes.Length == 0)
                    {
                        lblMessage.Text = "File bạn chọn bị lỗi! dung lượng file = " + FileUploadImage.FileBytes.Length.ToString() + " bytes";
                        return;
                    }
                    // 2. The uploaded file should be having one of the allowed extensions
                    string ext = System.IO.Path.GetExtension(FileUploadImage.FileName).TrimStart(".".ToCharArray()).ToLower();
                    if ((ext != "jpeg") && (ext != "jpg") && (ext != "png") && (ext != "gif") && (ext != "bmp") && (ext != "swf"))
                    {
                        lblMessage.Text = "Định dạng file bạn chọn không hợp lệ (File hợp lệ dạng Ảnh hoặc Flash)";
                        return;
                    }

                    // Validation successful
                    // Load the image into Bitmap Object

                    Bitmap uploadedImage = new Bitmap(FileUploadImage.FileContent);

                    // Set the maximum width and height here.
                    // You can make this versatile by getting these values from
                    // QueryString or textboxes
                    int maxWidth = 170;
                    int maxHeight = 160;

                    // Resize the image
                    Bitmap resizedImage = CommonBLL.GetScaledPicture(uploadedImage, maxWidth, maxHeight);

                    //Save the image
                    // String virtualPath = "~/temp/" + System.Guid.NewGuid().ToString() + "." + ext;
                    //String tempFileName = Server.MapPath(virtualPath);
                    resizedImage.Save(filepathsmallimage, uploadedImage.RawFormat);
                }
                catch { }
            }
            new CmsBLL().Category_InsertItem(0, txtTitle.Text.Trim(), strFileName, txtUrl.Text.Trim(), int.Parse("0" + rdActive.SelectedValue));
            lblMessage.Text = "Thêm mới thành công!";
            //LoadDataToDropDownList(DropdownParentID, 0);
            ResetForm();
        }
        catch (Exception ex)
        {
            lblMessage.Text = "ERROR! " + ex.Message.ToString();
        }
    }
    private void ResetForm()
    {
       // DropdownParentID.SelectedValue = "0";
        txtTitle.Text = "";   
        txtUrl.Text = "";     
        rdActive.SelectedValue = "1";
        txtTitle.Focus();
    }
}
